package com.hpe.wx.performance.redis.dao;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.hpe.wx.performance.redis.utils.RedisPool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Tuple;

@Repository
public class PerformanceDaoImpl implements PerformanceDao {
	private static Logger logger = Logger.getLogger(PerformanceDaoImpl.class.getSimpleName());
	private JedisPool pool = RedisPool.getPool();

	// @Autowired
	// private RedisTemplate<String, String> template;

	@Override
	public String add(String key, String value) {
		Jedis jedis = pool.getResource();
		String res = jedis.set(key, value);
		logger.info("###add    key:" + key + " value:" + value);
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public Long remove(String key) {
		Jedis jedis = pool.getResource();
		Long res = jedis.del(key);
		RedisPool.returnResource(pool, jedis);
		logger.info("###remove the Key: " + key + " the deleted count keys is: " + res);
		return res;
	}

	@Override
	public String getValueByKey(String key) {
		Jedis jedis = pool.getResource();
		String res = jedis.get(key);
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public Long zAdd(String key, double score, String member) {
		Jedis jedis = pool.getResource();
		logger.info("###zAdd    key:" + key + " score:" + score + " member:" + member);
		Long res = jedis.zadd(key, score, member);
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public Long zRemove(String key, String member) {
		Jedis jedis = pool.getResource();
		Long res = jedis.zrem(key, member);
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public Set<Tuple> popSortedSet(String key, int sort) {
		Set<Tuple> res = getSortedSetByIndex(key, 0, 0, sort);
		return res;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hpe.wx.performance.dao.PerformanceDao#getSortedSet(java.lang.String,
	 * long, long)
	 */
	@Override
	public Set<Tuple> getSortedSetByIndex(String key, long indexStart, long indexEnd, int sortedBy) {
		Jedis jedis = pool.getResource();
		Set<Tuple> keyElements = null;
		logger.info("###getSortedSetByIndex    key:" + key + " indexStart:" + indexStart + " indexEnd:" + indexEnd);
		if (sortedBy == 0) {
			keyElements = jedis.zrangeWithScores(key, indexStart, indexEnd);// score 
		} else {
			keyElements = jedis.zrevrangeWithScores(key, indexStart, indexEnd);// score
		}
		RedisPool.returnResource(pool, jedis);
		return keyElements;
	}

	@Override
	public Set<Tuple> getSortedSetByScore(String key, double scoreStart, double scoreEnd, int sortedBy) {
		Set<Tuple> res = null;
		Jedis jedis = pool.getResource(); 
		if (sortedBy == 0) {
			res = jedis.zrangeByScoreWithScores(key, scoreStart, scoreEnd);
		} else {
			res = jedis.zrangeByScoreWithScores(key, scoreStart, scoreEnd);
		} 
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public Set<Tuple> getSortedSetByScore(String key, String scoreStart, String scoreEnd, int sortedBy) {
		Set<Tuple> res = null;
		Jedis jedis = pool.getResource(); 
		if (sortedBy == 0) {
			res = jedis.zrangeByScoreWithScores(key, scoreStart, scoreEnd);
		} else {
			res = jedis.zrangeByScoreWithScores(key, scoreStart, scoreEnd);
		}
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public Long lPush(String key, String value) {
		Jedis jedis = pool.getResource();
		Long res = jedis.lpush(key, value);
		RedisPool.returnResource(pool, jedis);
		return res;
	}

	@Override
	public List<String> lGet(String key, int indexStart, int indexEnd) {
		List<String> res = null;
		Jedis jedis = pool.getResource();
		res = jedis.lrange(key, indexStart, indexEnd);
		return res;
	}

}
